<template>
  <div class="login-container">
    <div class="login-background">
      <div class="pet-animation">
        <div class="paw-print paw1"></div>
        <div class="paw-print paw2"></div>
        <div class="paw-print paw3"></div>
        <div class="paw-print paw4"></div>
      </div>
    </div>
    
    <div class="login-box">
      <div class="login-header">
        <h2>伴侣宠物之家系统</h2>
        <p class="subtitle">爱宠生活，从这里开始</p>
      </div>
      
      <el-form :model="loginForm" :rules="loginRules" ref="loginForm" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user"
            placeholder="请输入用户名"
            clearable
          ></el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            clearable
            @keyup.enter.native="handleLogin"
          ></el-input>
        </el-form-item>
        
        <el-form-item prop="captcha">
          <div class="captcha-container">
            <el-input
              v-model="loginForm.captcha"
              prefix-icon="el-icon-picture"
              placeholder="请输入验证码"
              clearable
              maxlength="4"
              @keyup.enter.native="handleLogin"
              style="width: 60%;"
            ></el-input>
            <div class="captcha-image" @click="refreshCaptcha">
              <img :src="captchaUrl" alt="验证码" />
              <div class="refresh-hint">点击刷新</div>
            </div>
          </div>
        </el-form-item>
        
        <el-form-item prop="role">
          <div class="role-selector">
            <div
              class="role-option"
              :class="{ active: loginForm.role === '普通用户' }"
              @click="loginForm.role = '普通用户'"
            >
              <i class="el-icon-user"></i>
              <span>普通用户</span>
            </div>
            <div
              class="role-option"
              :class="{ active: loginForm.role === '管理员' }"
              @click="loginForm.role = '管理员'"
            >
              <i class="el-icon-s-custom"></i>
              <span>管理员</span>
            </div>
          </div>
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="handleLogin"
            class="login-button"
          >
            {{ loading ? '登录中...' : '登 录' }}
          </el-button>
        </el-form-item>
        
        <div class="register-link">
          还没有账号？
          <span @click="goToRegister">立即注册</span>
        </div>
      </el-form>
    </div>
    
    <!-- 测试账号卡片 - 左上角固定 -->
    <div class="test-accounts-card" v-if="testAccounts.length > 0">
      <div class="card-header" @click="toggleCard">
        <i class="el-icon-info"></i>
        <span>测试账号</span>
        <i :class="isCardExpanded ? 'el-icon-arrow-up' : 'el-icon-arrow-down'" class="toggle-icon"></i>
      </div>
      <transition name="slide-fade">
        <div class="card-content" v-show="isCardExpanded">
          <div 
            class="account-item"
            v-for="(account, index) in testAccounts"
            :key="index"
            @click="quickFill(account)"
          >
            <div class="account-role">
              <i :class="account.icon"></i>
              <span>{{ account.role }}</span>
            </div>
            <div class="account-info">
              <span class="value">{{ account.username }} / {{ account.password }}</span>
            </div>
          </div>
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
import { login, getTestAccounts } from '@/api/user';

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        captcha: '',
        role: '普通用户'
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        captcha: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ],
        role: [
          { required: true, message: '请选择登录身份', trigger: 'change' }
        ]
      },
      loading: false,
      captchaUrl: '',
      testAccounts: [],
      isCardExpanded: true
    };
  },
  mounted() {
    this.refreshCaptcha();
    this.loadTestAccounts();
  },
  methods: {
    refreshCaptcha() {
      this.captchaUrl = `http://47.97.154.151:18001/api/captcha/generate?t=${Date.now()}`;
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          login(this.loginForm.username, this.loginForm.password, this.loginForm.role, this.loginForm.captcha)
            .then(response => {
              this.loading = false;
              const result = response.data;
              
              if (result.success) {
                // 保存用户信息到本地存储
                localStorage.setItem('userInfo', JSON.stringify(result.data));
                
                this.$notify({
                  title: '成功',
                  message: result.message,
                  type: 'success',
                  duration: 2000
                });
                
                // 根据角色跳转不同页面
                setTimeout(() => {
                  if (result.data.role === '管理员') {
                    this.$router.push('/admin/home');
                  } else {
                    this.$router.push('/user/home');
                  }
                }, 500);
              } else {
                // 登录失败，刷新验证码
                this.refreshCaptcha();
                this.loginForm.captcha = '';
                this.$notify({
                  title: '错误',
                  message: result.message,
                  type: 'error',
                  duration: 3000
                });
              }
            })
            .catch(error => {
              this.loading = false;
              this.refreshCaptcha();
              this.loginForm.captcha = '';
              this.$notify({
                title: '错误',
                message: '登录请求失败，请检查网络连接',
                type: 'error',
                duration: 3000
              });
              console.error('登录错误:', error);
            });
        }
      });
    },
    goToRegister() {
      this.$router.push('/register');
    },
    quickFill(account) {
      this.loginForm.username = account.username;
      this.loginForm.password = account.password;
      this.loginForm.role = account.roleType;
      
      this.$notify({
        title: '提示',
        message: `已填充${account.role}测试账号`,
        type: 'success',
        duration: 2000
      });
    },
    loadTestAccounts() {
      getTestAccounts()
        .then(response => {
          if (response.data.success && response.data.data) {
            this.testAccounts = response.data.data.map(account => ({
              ...account,
              icon: this.getRoleIcon(account.roleType)
            }));
          }
        })
        .catch(error => {
          console.log('获取测试账号失败，使用默认账号', error);
          // 如果后端接口不存在，使用默认测试账号
          this.testAccounts = [
            {
              role: '管理员',
              username: 'admin',
              password: '123456',
              icon: 'el-icon-s-custom',
              roleType: '管理员'
            },
            {
              role: '普通用户',
              username: 'user',
              password: '123456',
              icon: 'el-icon-user',
              roleType: '普通用户'
            }
          ];
        });
    },
    getRoleIcon(role) {
      // 根据不同角色返回不同图标
      const iconMap = {
        '管理员': 'el-icon-s-custom',
        '普通用户': 'el-icon-user',
        '医生': 'el-icon-user-solid',
        '护士': 'el-icon-service',
        '员工': 'el-icon-s-check',
        '商家': 'el-icon-s-shop',
        '志愿者': 'el-icon-star-on'
      };
      return iconMap[role] || 'el-icon-user';
    },
    toggleCard() {
      this.isCardExpanded = !this.isCardExpanded;
    }
  }
};
</script>

<style scoped>
.login-container {
  position: relative;
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #2c5f2d 0%, #97bc62 100%);
  overflow: hidden;
}

.login-background {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
}

.pet-animation {
  width: 100%;
  height: 100%;
  position: relative;
}

.paw-print {
  position: absolute;
  width: 40px;
  height: 40px;
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="rgba(255,255,255,0.1)"><path d="M12 2c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm6 8c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm-12 0c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm6 6c-2.2 0-4 1.8-4 4h8c0-2.2-1.8-4-4-4z"/></svg>');
  background-size: contain;
  animation: pawAnimation 20s infinite;
  opacity: 0;
}

.paw1 {
  top: 10%;
  left: 20%;
  animation-delay: 0s;
}

.paw2 {
  top: 30%;
  right: 15%;
  animation-delay: 5s;
}

.paw3 {
  bottom: 20%;
  left: 15%;
  animation-delay: 10s;
}

.paw4 {
  bottom: 10%;
  right: 20%;
  animation-delay: 15s;
}

@keyframes pawAnimation {
  0%, 100% {
    opacity: 0;
    transform: scale(0.5) rotate(0deg);
  }
  50% {
    opacity: 1;
    transform: scale(1) rotate(360deg);
  }
}

.login-box {
  position: relative;
  width: 420px;
  background: white;
  border-radius: 16px;
  padding: 40px 50px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
  animation: slideIn 0.6s ease-out;
  z-index: 1;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 35px;
}

.pet-icon {
  font-size: 50px;
  margin-bottom: 10px;
  animation: bounce 2s infinite;
}

@keyframes bounce {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-10px);
  }
}

.login-header h2 {
  color: #2c5f2d;
  font-size: 26px;
  font-weight: 600;
  margin: 10px 0;
}

.subtitle {
  color: #97bc62;
  font-size: 14px;
  margin: 5px 0 0 0;
}

.login-form {
  margin-top: 20px;
}

.login-form >>> .el-input__inner {
  height: 45px;
  border-radius: 8px;
  border: 1px solid #e0e0e0;
  transition: all 0.3s;
}

.login-form >>> .el-input__inner:focus {
  border-color: #97bc62;
  box-shadow: 0 0 0 2px rgba(151, 188, 98, 0.1);
}

.role-selector {
  display: flex;
  gap: 15px;
  width: 100%;
}

.role-option {
  flex: 1;
  height: 70px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  transition: all 0.3s;
  background: white;
}

.role-option:hover {
  border-color: #97bc62;
  background: #f8faf5;
}

.role-option.active {
  border-color: #2c5f2d;
  background: linear-gradient(135deg, #2c5f2d 0%, #97bc62 100%);
  color: white;
}

.role-option i {
  font-size: 24px;
}

.role-option span {
  font-size: 14px;
  font-weight: 500;
}

.login-button {
  width: 100%;
  height: 45px;
  background: linear-gradient(135deg, #2c5f2d 0%, #97bc62 100%);
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  transition: all 0.3s;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(44, 95, 45, 0.3);
}

.register-link {
  text-align: center;
  color: #666;
  font-size: 14px;
  margin-top: 10px;
}

.register-link span {
  color: #2c5f2d;
  cursor: pointer;
  font-weight: 600;
  transition: color 0.3s;
}

.register-link span:hover {
  color: #97bc62;
  text-decoration: underline;
}

.captcha-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.captcha-image {
  width: 38%;
  height: 45px;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  position: relative;
  border: 1px solid #e0e0e0;
  transition: all 0.3s;
}

.captcha-image:hover {
  border-color: #97bc62;
  box-shadow: 0 0 0 2px rgba(151, 188, 98, 0.1);
}

.captcha-image img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

.refresh-hint {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.6);
  color: white;
  font-size: 10px;
  text-align: center;
  padding: 2px 0;
  opacity: 0;
  transition: opacity 0.3s;
}

.captcha-image:hover .refresh-hint {
  opacity: 1;
}

/* 测试账号卡片样式 - 左上角固定 */
.test-accounts-card {
  position: absolute;
  top: 20px;
  left: 20px;
  min-width: 200px;
  max-width: 280px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  animation: slideInLeft 0.5s ease-out;
  z-index: 100;
  overflow: hidden;
}

@keyframes slideInLeft {
  from {
    opacity: 0;
    transform: translateX(-20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.test-accounts-card .card-header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 12px;
  background: linear-gradient(135deg, #2c5f2d 0%, #97bc62 100%);
  cursor: pointer;
  user-select: none;
  transition: all 0.3s;
}

.test-accounts-card .card-header:hover {
  background: linear-gradient(135deg, #234a24 0%, #8aad57 100%);
}

.test-accounts-card .card-header i {
  font-size: 14px;
  color: white;
}

.test-accounts-card .card-header span {
  font-size: 13px;
  font-weight: 600;
  color: white;
  flex: 1;
}

.test-accounts-card .card-header .toggle-icon {
  font-size: 12px;
  color: white;
  transition: transform 0.3s;
}

.test-accounts-card .card-content {
  padding: 8px;
  background: white;
  max-height: 400px;
  overflow-y: auto;
}

.test-accounts-card .card-content::-webkit-scrollbar {
  width: 6px;
}

.test-accounts-card .card-content::-webkit-scrollbar-thumb {
  background: #97bc62;
  border-radius: 3px;
}

.test-accounts-card .card-content::-webkit-scrollbar-track {
  background: #f0f0f0;
  border-radius: 3px;
}

.account-item {
  background: linear-gradient(135deg, #f8faf5 0%, #ffffff 100%);
  border: 1px solid #e8f0e8;
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.account-item:last-child {
  margin-bottom: 0;
}

.account-item:hover {
  border-color: #97bc62;
  background: linear-gradient(135deg, #f0f7e8 0%, #ffffff 100%);
  transform: translateX(3px);
  box-shadow: 0 2px 8px rgba(151, 188, 98, 0.2);
}

.account-role {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-bottom: 4px;
}

.account-role i {
  font-size: 14px;
  color: #2c5f2d;
}

.account-role span {
  font-size: 12px;
  font-weight: 600;
  color: #2c5f2d;
}

.account-info {
  padding-left: 19px;
}

.account-info .value {
  color: #555;
  font-size: 11px;
  font-family: 'Courier New', monospace;
  background: rgba(255, 255, 255, 0.8);
  padding: 2px 6px;
  border-radius: 4px;
  border: 1px solid #e0e0e0;
  display: inline-block;
}

/* 卡片展开/收起动画 */
.slide-fade-enter-active {
  transition: all 0.3s ease;
}

.slide-fade-leave-active {
  transition: all 0.2s ease;
}

.slide-fade-enter,
.slide-fade-leave-to {
  max-height: 0;
  opacity: 0;
  overflow: hidden;
}

.slide-fade-enter-to,
.slide-fade-leave {
  max-height: 300px;
  opacity: 1;
}
</style>

